﻿#include "Saver.h"
#include "Player.h"
#include "BackPack.h"
#include "Equipment.h"
#include "Drug.h"
#include "Special.h"
#include "Skill.h"
#include <iostream>

// 打印玩家信息（中文）
void printPlayer(const Player& player) {
    std::cout << "\n=== 玩家信息 ===\n";
    std::cout << "姓名: " << player.getName() << "\n";
    std::cout << "等级: " << player.getLevel() << "\n";
    std::cout << "生命值: " << player.getCurrentHealth() << "/" << player.getMaxHealth() << "\n";
    std::cout << "法力值: " << player.getCurrentMana() << "/" << player.getMaxMana() << "\n";
    std::cout << "力量: " << player.getStrength() << "\n";
    std::cout << "敏捷: " << player.getAgility() << "\n";
    std::cout << "防御: " << player.getDefense() << "\n";
    std::cout << "幸运: " << player.getLuck() << "\n";
    std::cout << "智慧: " << player.getWisdom() << "\n";
    std::cout << "命中: " << player.getAccuracy() << "\n";
    std::cout << "闪避: " << player.getDodging() << "\n";
    std::cout << "金钱: " << player.getMoney() << "\n";
    std::cout << "经验值: " << player.getExpoints() << "\n";
    std::cout << "理智值: " << player.getSan() << " (恢复速度: " << player.getSanSpeed() << ")\n";

    std::cout << "\n技能列表:\n";
    for (const auto& skill : player.getSkills()) {
        std::cout << " - " << skill.getName()
            << " (伤害:" << skill.getDamage()
            << ", 消耗法力:" << skill.getManaCost() << ")\n";
    }

    std::cout << "\n背包 (" << player.getBackpack().getCurrentCount()
        << "/" << player.getBackpack().getMaxCapacity() << " 个物品):\n";
    for (int i = 0; i < player.getBackpack().getCurrentCount(); ++i) {
        const Item* item = player.getBackpack().getItemByIndex(i);
        if (item) {
            std::string type;
            if (dynamic_cast<const Equipment*>(item)) type = "装备";
            else if (dynamic_cast<const Drug*>(item)) type = "药品";
            else if (dynamic_cast<const Special*>(item)) type = "特殊物品";
            else type = "未知";

            std::cout << " [" << i << "] " << item->getName()
                << " (类型: " << type
                << ", 价值: " << item->getValue() << ")\n";
        }
    }
    std::cout << "\n";
}

int main() {
    const std::string saveFile = "test_save.json";

    // 创建原始玩家
    Player player("Alice");
    player.setLevel(5);
    player.setMaxHealth(100);
    player.heal(80); // 血量为80
    player.setMaxMana(50);
    player.restoreMana(45);
    player.setStrength(10);
    player.setAgility(8);
    player.setDefense(7);
    player.setLuck(6);
    player.setWisdom(9);
    player.setAccuracy(15);
    player.setDodging(12);
    player.setMoney(200);
    player.setExpoints(450);
    player.setSan(90);
    player.setSanSpeed(2);

    // 添加技能
    Skill fireball("火球术", "释放一团火焰攻击敌人", 25, 10, true, StatusEffect::BURN, 30, 3);
    Skill heal("治疗术", "恢复自身生命值", 0, 15, false, StatusEffect::HEAL, 100, 0);
    player.addSkill(fireball);
    player.addSkill(heal);

    // 背包和物品
    BackPack backpack(10);

    Equipment* sword = new Equipment();
    sword->setName("铁剑");
    sword->setValue(50);
    sword->setPower(5);
    sword->setSlot(EquipSlot::WEAPON);
    sword->setConsume(2);

    Drug* potion = new Drug("小红药", 30, 'H', 20);
    Special* key = new Special("金色钥匙", "开启隐藏之门", false);

    backpack.addItem(sword);
    backpack.addItem(potion);
    backpack.addItem(key);

    player.setBackpack(backpack);

    std::cout << "保存前的玩家状态:\n";
    printPlayer(player);

    // 保存游戏
    std::cout << "正在保存游戏到 '" << saveFile << "'...\n";
    if (Saver::saveGame(player, saveFile)) {
        std::cout << "保存成功！\n";
    }
    else {
        std::cerr << "保存失败！\n";
        return 1;
    }

    // 加载游戏
    Player loadedPlayer("默认角色");
    std::cout << "\n正在从存档加载玩家...\n";
    if (Saver::loadGame(loadedPlayer, saveFile)) {
        std::cout << "加载成功！\n";
    }
    else {
        std::cerr << "加载失败！\n";
        return 1;
    }

    // 显示加载结果
    std::cout << "加载后的玩家状态:\n";
    printPlayer(loadedPlayer);

    // 验证
    if (loadedPlayer.getName() == "Alice" && loadedPlayer.getLevel() == 5) {
        std::cout << "测试通过：玩家数据完整无误地被保存并加载！\n";
    }
    else {
        std::cout << "测试失败：数据不一致。\n";
    }

    return 0;
}